package cn.edu.nuc.article.util;

import java.io.*;
public class SwapStream {
    //    java 输出流转化为输入流
//new ByteArrayInputStream(byteArrayOutputStream.toByteArray());


    /**
     * 输入流转byte[ ]
     *
     * @param inStream
     * @return
     * @throws IOException
     */
    private static final byte[] input2byte(InputStream inStream)
            throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        int rc = 0;
        while ((rc = inStream.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] in2b = swapStream.toByteArray();
        return in2b;
    }


    /**
     * @param 字节数组，输出文件流 转file对象
     * @param outputFile
     * @return
     */
    public static File writeBytesToFile(byte[] b, String outputFile) {
        File file = null;
        FileOutputStream os = null;
        try {
            file = new File(outputFile);
            os = new FileOutputStream(file);
            os.write(b);
        } catch (Exception var13) {
            var13.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
            } catch (IOException var12) {
                var12.printStackTrace();
            }
        }
        return file;
    }

    // outputStream转inputStream
    public static InputStream parse(OutputStream out) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos = (ByteArrayOutputStream) out;
        InputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
        return swapStream;
    }
}
